package com.sevenit.nj.yrmtfish.common;

import com.sevenit.nj.yrmtfish.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.ImageView;

/**  
*  @description: 自动拉伸图片高度来适应View宽度的类.
*  @version 1.0  
*  @author wangxw
*/  
public class ImageViewFitW extends ImageView {
	
	//定义高度和宽度的比例，如果值为0.0，则高度自动适应图片.
	float height_k = 0.0f;
	
    /**
     * 构造函数.
     * @param context
     */
    public ImageViewFitW(Context context) {
        super(context);
    }
    
    /**
     * 构造函数.
     * @param context
     * @param attrs
     */
    public ImageViewFitW(Context context, AttributeSet attrs) {
        super(context, attrs);
        
        //从属性集中取得属性列表
  		TypedArray a = context.obtainStyledAttributes(attrs,  R.styleable.ImageViewFitW);  
  		height_k = a.getFloat(R.styleable.ImageViewFitW_height_k, 0.0f);
    }
    
    /**
     * 构造函数.
     * @param context
     * @param attrs
     * @param defStyle
     */
    public ImageViewFitW(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    
    @Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		
		int imgWidth = getDrawable().getIntrinsicWidth();
		int	imgHeight = getDrawable().getIntrinsicHeight();
		int currentWidth = getMeasuredWidth();
        int currentHeight = getMeasuredHeight();
        
		if(height_k < 0.001){
			//高度自动适应图片.
	        currentHeight = (int)((float)currentWidth / (float)imgWidth * (float)imgHeight);
		}else{
			//高度和高度比例一定.
			currentHeight = (int)((float)currentWidth * height_k);
		}
		
		setMeasuredDimension(currentWidth, currentHeight);
	}
}
